
<#if chaptSectionsMap??>
<#list chaptSectionsMap as key, chapt>
<div class="chapter">
    <h3>
        <strong>${chapt.chaptName!} (${chapt.time!}分钟)</strong>
        <a href="javascprit:void(0)" onclick="deleteSection('${chapt.chaptId!}')" style="margin-right: 20px;" class="chapter-edit">删除</a>
        <a href="javascprit:void(0)" onclick="eidtSection('${chapt.chaptId!}','0')" class="chapter-edit">修改</a>
        <a href="javascprit:void(0)" onclick="moveSection('${chapt.chaptId!}','down')" class="chapter-edit">下移</a>
        <a href="javascprit:void(0)" onclick="moveSection('${chapt.chaptId!}','up')" class="chapter-edit">上移</a>
    </h3>

    <ul class="chapter-sub">
        <#if chapt.sections??>
        <#list chapt.sections as section>
        <li class="chapter-sub-li">
            <span>${section.name!} (${section.time!}分钟)</span>
            <a href="javascprit:void(0)" onclick="deleteSection('${section.id!}')" class="chapter-edit">删除</a>
            <a href="javascprit:void(0)" onclick="eidtSection('${section.id!}','${section.parentId!}')" class="chapter-edit">修改</a>
            <a href="javascprit:void(0)" onclick="moveSection('${section.id!}','down')" class="chapter-edit">下移</a>
            <a href="javascprit:void(0)" onclick="moveSection('${section.id!}','up')" class="chapter-edit">上移</a>
        </li>
        </#list>
        </#if>
    </ul>
</div>
</#list>
</#if>

<!-- 章节弹出层 -start -->
<div class="modal" id="chaptSectionModal" tabindex="-1" role="dialog"  aria-hidden="true">
    <div class="modal_wapper">
        <div class="modal-dialog w-8" >
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" >修改课程章节</h4>
                </div>
                <div class="modal-body">
                    <form role="form" id="chaptSectionForm" method="post" action="${base}/updateSection">
                        <input type="hidden" name="id" value=""/>
                        <div class="form-group clearfix">
                            <label class="control-label" >名称</label>
                            <div class="col-sm-8">
                                <input type="text" name="name" class="form-control" placeholder="请输入名称">
                            </div>
                        </div>
                        <div class="form-group clearfix" id="videoUrl-group">
                            <label class="control-label" >视频地址</label>
                            <div class="col-sm-8">
                                <input type="text" name="videoUrl" class="form-control" placeholder="请输入url">
                            </div>
                        </div>
                        <div class="form-group clearfix" id="time-group">
                            <label class="control-label" >时长</label>
                            <div class="col-sm-8">
                                <input type="number" name="time" oninput="if(value<0)value=0;if(value>10000)value=10000" class="form-control" placeholder="请输入分钟数">
                            </div>
                        </div>
                    </form>
                </div>

                <div class="modal-footer">
                    <button type="button" onclick="saveSection()" class="btn btn-primary" >保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- 章节弹出层 -end -->
<script type="text/javascript">
    function  eidtSection(id, parentId) {
        if(parentId == '0'){//修改章
            $('#videoUrl-group').hide();
            $('#time-group').hide();
        }else{//修改节
            $('#videoUrl-group').show();
            $('#time-group').show();
        }
        $.ajax({
            url:'${base}/getSection',
            type:'POST',
            dataType:'json',
            data:{'id': id},
            success: function (resp) {
                if(resp.errcode == 0){
                    Modal.show('chaptSectionModal');
                    $('#chaptSectionForm').fill(resp.data);
                }else{
                    Modal.alert('获取失败');
                }
            }
        })
    }

    function saveSection() {
        $('#chaptSectionForm').ajaxSubmit({
            dataType:'json',
            success: function (resp) {
                if(resp.errcode == 0){
                    window.location.reload();
                }else{
                    Modal.alert('保存失败');
                }
            }
        })
    }

    //移动;down:下移；up：上移
    function moveSection(id, direct) {
        $.ajax({
            url:'${base}/moveSection',
            dataType:'json',
            data:{'id':id, 'direct':direct},
            success: function (resp) {
                if(resp.errcode == 0){
                    window.location.reload();
                }else{
                    Modal.alert('移动失败');
                }
            }
        });
    }
    //删除章节
    function deleteSection(id) {
        Modal.confirm("确认删除？",function () {
            $.ajax({
                url:'${base}/deleteSection',
                dataType:'json',
                data:{'id':id},
                success: function (resp) {
                    if(resp.errcode == 0){
                        window.location.reload();
                    }else{
                        Modal.alert('删除失败');
                    }
                }
            });
        })
    }

</script>